<?php
class Navigation{
	
	public static $naviTemplates = array();
	public static $htaccess = array();
	
	public function __construct() {
	}
	
	
	public static function getNavigations() {
		$navis = array();
		$query = DB::getInstance()->query("
			SELECT 
				id,
				naviName
			FROM
				".Tables::$naviTbl."");
		while($navi =DB::getInstance()->fetchAssoc($query)) {
			
			$navis[$navi['id']] = $navi;
		}
		
		return $navis;
	}
	
	public static function getNavigationCats() {
		$navCats = array();
		$query = DB::getInstance()->query("
			SELECT 
				".Tables::$naviTbl.".id as naviId,
				".Tables::$naviTbl.".naviName,
				".Tables::$naviCatTbl.".id as catId,
				".Tables::$naviCatTbl.".naviId as catNaviId,
				".Tables::$naviCatTbl.".catName,
				".Tables::$naviCatTbl.".catShowTab,
				".Tables::$naviCatTbl.".catOnlyAdmin
			FROM
				".Tables::$naviCatTbl."
				LEFT JOIN 
					".Tables::$naviTbl." 
				ON 
					(".Tables::$naviCatTbl.".naviId = ".Tables::$naviTbl.".id)
			WHERE
				".Tables::$naviCatTbl.".catShow = 0
			ORDER BY
				orderId ASC");
		while($cats = DB::getInstance()->fetchAssoc($query)) {
			if($cats['catOnlyAdmin'] == 0 && General::getInstance()->getGlobalVar('isAdmin') == true ||
				$cats['catOnlyAdmin'] == 1) {
					
				$display = false;
				if($cats['catShowTab'] != "") {
					$tabs = explode(",",$cats['catShowTab']);
					
					// No Tab saved
					if((!isset($_SESSION[Settings::getInstance()->getSetting("General.sqlPre").'tab']) || 
						$_SESSION[Settings::getInstance()->getSetting("General.sqlPre").'tab'] == "") &&
						in_array("-1",$tabs)) {
							
							$display = true;
					}
					
					if(isset($_SESSION[Settings::getInstance()->getSetting("General.sqlPre").'tab']) &&
						in_array($_SESSION[Settings::getInstance()->getSetting("General.sqlPre").'tab'],$tabs)) {
							$display = true;
					}
					//$_SESSION[Settings::getInstance()->getSetting("General.sqlPre").'tab']
				}else{
					$display = true;
				}
				
				
				$catData = array();
				$catData['name'] = $cats['catName'];
				
				// Save Data
				if($display == true) $navCats[$cats['naviId']][$cats['catId']] = $catData;
			}
		}
		General::getTpl()->assign("navCats",$navCats);
	}
	
	public static function getNavigationCatsByNavId($navId) {
		if(!is_numeric($navId)) return false;
		
		$cats = array();
		$query = DB::getInstance()->query("
			SELECT
				id,
				catName,
				catShow,
				orderId
			FROM
				".Tables::$naviCatTbl."
			WHERE
				naviId = '".$navId."'
			ORDER BY
				orderId ASC");
		while($c = DB::getInstance()->fetchAssoc($query)) {
			$cats[] = $c;
		}
		return $cats;
	}
	
	public static function getAllNavigationCats() {
		$cats = array();
		$query = DB::getInstance()->query("
			SELECT
				id,
				catName
			FROM
				".Tables::$naviCatTbl."
			ORDER BY
				orderId ASC");
		while($c = DB::getInstance()->fetchAssoc($query)) {
			$cats[$c['id']] = $c['catName'];
		}
		return $cats;
	}
	
	public static function getLinks() {
		$links = array();
		$query = DB::getInstance()->query("
			SELECT
				".Tables::$naviLinksTbl.".id,
				".Tables::$naviLinksTbl.".linkCatId,
				".Tables::$naviLinksTbl.".linkName,
				".Tables::$naviLinksTbl.".linkURL,
				".Tables::$naviLinksTbl.".linkTemplate,
				".Tables::$naviLinksTbl.".linkHTML,
				".Tables::$naviLinksTbl.".linkOnlyAdmin,
				".Tables::$naviLinksTbl.".linkTab,
				".Tables::$naviCatTbl.".catOnlyAdmin
			FROM
				".Tables::$naviLinksTbl."
				LEFT JOIN 
					".Tables::$naviCatTbl."
				ON
					(".Tables::$naviLinksTbl.".linkCatId = ".Tables::$naviCatTbl.".id)
			WHERE
				".Tables::$naviLinksTbl.".linkShow = 0
			ORDER BY
				".Tables::$naviLinksTbl.".orderId ASC");
		while($linksData = DB::getInstance()->fetchAssoc($query)) {
			if(($linksData['linkOnlyAdmin'] == 0 && General::getInstance()->getGlobalVar('isAdmin') == true ||
				$linksData['linkOnlyAdmin'] == 1) &&
				($linksData['catOnlyAdmin'] == 0 && General::getInstance()->getGlobalVar('isAdmin') == true ||
				$linksData['catOnlyAdmin'] == 1)) {
				if($linksData['linkTemplate'] != "") {
					if(in_array($linksData['linkTemplate'],Navigation::$naviTemplates)) {
						if(file_exists($linksData['linkTemplate'])) include($linksData['linkTemplate']);
					}
				}else{
					if(Settings::getInstance()->getSetting("general.useModRewrite") == 0) {
						// Mod Rewrite is on
						// Add the CMS-Url if it's an internal link ( no http://)
						if (strtolower(substr($linksData['linkURL'], 0, 7)) != "http://" &&
								strtolower(substr($linksData['linkURL'], 0, 3)) != "www") {
							$linksData['linkURL'] = Settings::getInstance()->getSetting("General.urlToCMS").$linksData['linkURL'];		
						}
					}
						
					// Add ?tab=... if necessary
					if($linksData['linkTab'] == 0) {
						if(strpos($linksData['linkURL'],"?") === false) {
				        	$linksData['linkURL'] .= "?tab=".$linksData['id'];
				        }else{
				        	$linksData['linkURL'] .= "&tab=".$linksData['id'];
				        }
					}
					
					$links[$linksData['linkCatId']][] = $linksData;
				}
			}
		}
		
		General::getTpl()->assign("links",$links);
	}
	
	public static function getLinksByCatId($catId) {
		if(!is_numeric($catId)) exit;
		
		$links = array();
		$query = DB::getInstance()->query("
			SELECT
				id,
				linkCatId,
				linkName,
				linkURL,
				linkTemplate,
				linkHTML,
				linkTab,
				linkShow
			FROM
				".Tables::$naviLinksTbl."
			WHERE
				linkCatId = '".$catId."'
			ORDER BY
				orderId ASC");
		while($l = DB::getInstance()->fetchAssoc($query)) {
			$l['parsedLinkTemplate'] = "";
			if($l['linkTemplate'] != "") {
				if(in_array($l['linkTemplate'],Navigation::$naviTemplates)) {
					$key = array_search($l['linkTemplate'],Navigation::$naviTemplates);
					$l['parsedLinkTemplate'] = $key;
				}else{
					$l['parsedLinkTemplate'] = $l['linkTemplate'];
				}
			}
			$links[] = $l;
		}
		return $links;
	}
	
	
	public static function getTabs() {
		$tabs = array();
		$query = DB::getInstance()->query("
			SELECT
				id,
				linkName
			FROM
				".Tables::$naviLinksTbl."
			WHERE
				linkTab = '0'");
		while($t = DB::getInstance()->fetchAssoc($query)) {
			$tabs[] = $t;
		}
		return $tabs;
	}
	
	
	
	public static function outputNavigation() {
		Navigation::getNavigationCats();
		Navigation::getLinks();
	}
	
	
	/**
	 * ADMIN- Create navigation
	 */
	public static function createNavigation($naviName) {
		DB::getInstance()->query("
			INSERT INTO
				".Tables::$naviTbl."
			SET
				naviName = '".DB::parseVar($naviName)."'");
	}
	
	/**
	 * ADMIN- Edit navigation
	 */
	public static function editNavigation($naviId,$naviName) {
		DB::getInstance()->query("
			UPDATE
				".Tables::$naviTbl."
			SET
				naviName = '".DB::parseVar($naviName)."'
			WHERE
				id = '".$naviId."'");
	}
	
	/**
	 * ADMIN- Delete Navigation
	 */
	public static function deleteNavigation($navId) {
		if(!is_numeric($navId)) exit;
		DB::getInstance()->query("
			DELETE FROM
				".Tables::$naviTbl."
			WHERE
				id = '".$navId."'");
		
		// Delete Categories/Links
		$cats = Navigation::getNavigationCatsByNavId($navId);
		foreach($cats as $data) {
			Navigation::deleteCategory($data['id']);
		}
	}
	
	/**
	 * ADMIN Create Nav-Category
	 */
	public static function createCategory($navId,$catName,$catShow,$catTab,$onlyAdmins) {
		if(!is_numeric($navId) || !is_numeric($catShow) || !is_numeric($onlyAdmins)) exit;
		
		// Get Order-Id
		$query = DB::getInstance()->query("
			SELECT
				id
			FROM
				".Tables::$naviCatTbl."
			WHERE
				naviId = '".$navId."'");
		$orderId = DB::getInstance()->numRows($query);
		
		DB::getInstance()->query("
			INSERT INTO
				".Tables::$naviCatTbl."
			SET
				naviId = '".$navId."',
				catName = '".DB::parseVar($catName)."',
				catShow = '".$catShow."',
				catShowTab = '".DB::parseVar($catTab)."',
				orderId = '".$orderId."',
				catOnlyAdmin = '".$onlyAdmins."'");
	}
	
	/**
	 * ADMIN Edit Nav-Category
	 */
	public static function editCategory($catId,$catName,$catShow,$catTab,$navId,$onlyAdmins) {
		if(!is_numeric($catId) || !is_numeric($catShow) || !is_numeric($navId) || !is_numeric($onlyAdmins)) exit;
		
		DB::getInstance()->query("
			UPDATE
				".Tables::$naviCatTbl."
			SET
				naviId = '".$navId."',
				catName = '".DB::parseVar($catName)."',
				catShow = '".$catShow."',
				catShowTab = '".DB::parseVar($catTab)."',
				catOnlyAdmin = '".$onlyAdmins."'
			WHERE
				id = '".$catId."'");
	}
	
	/**
	 * ADMIN Delete Nav-Category
	 */
	public static function deleteCategory($catId) {
		if(!is_numeric($catId)) exit;
		DB::getInstance()->query("
			DELETE FROM
				".Tables::$naviCatTbl."
			WHERE
				id = '".$catId."'");
		/**
		 * Delete Links
		 */
		DB::getInstance()->query("
			DELETE FROM
				".Tables::$naviLinksTbl."
			WHERE
				linkCatId = '".$catId."'");
	}
	
	
	/**
	 * ADMIN Create Link
	 */
	public static function createLink($catId,$linkName,$linkURL,$linkTemplate,$htmlCode,$linkShow,$isTab,$linkOnlyAdmin) {
		if(!is_numeric($catId) || !is_numeric($linkShow) || !is_numeric($linkOnlyAdmin)) exit;
		
		// Get Order-Id
		$query = DB::getInstance()->query("
			SELECT
				id
			FROM
				".Tables::$naviLinksTbl."
			WHERE
				linkCatId = '".$catId."'");
		$orderId = DB::getInstance()->numRows($query);
		
		DB::getInstance()->query("
			INSERT INTO
				".Tables::$naviLinksTbl."
			SET
				linkCatId = '".$catId."',
				linkName = '".DB::parseVar($linkName)."',
				linkURL = '".DB::parseVar($linkURL)."',
				linkTemplate = '".DB::parseVar($linkTemplate)."',
				linkHTML = '".DB::parseVar($htmlCode)."',
				linkShow = '".$linkShow."',
				linkTab = '".$isTab."',
				linkOnlyAdmin = '".$linkOnlyAdmin."',
				orderId = '".$orderId."'");
	}
	
	
	/**
	 * ADMIN Edit Link
	 */
	public static function editLink($linkId,$catId,$linkName,$linkURL,$linkTemplate,$htmlCode,$linkShow,$isTab,$linkOnlyAdmin) {
		if(!is_numeric($linkId) || !is_numeric($catId) || !is_numeric($linkShow) || !is_numeric($linkOnlyAdmin)) exit;
		
		DB::getInstance()->query("
			UPDATE
				".Tables::$naviLinksTbl."
			SET
				linkCatId = '".$catId."',
				linkName = '".DB::parseVar($linkName)."',
				linkURL = '".DB::parseVar($linkURL)."',
				linkTemplate = '".DB::parseVar($linkTemplate)."',
				linkHTML = '".DB::parseVar($htmlCode)."',
				linkShow = '".$linkShow."',
				linkTab = '".$isTab."',
				linkOnlyAdmin = '".$linkOnlyAdmin."'
			WHERE
				id = '".$linkId."'");
	}
	
	/**
	 * ADMIN Delete Link
	 */
	public static function deleteLink($linkId) {
		if(!is_numeric($linkId)) exit;
		DB::getInstance()->query("
			DELETE FROM
				".Tables::$naviLinksTbl."
			WHERE
				id = '".$linkId."'");
	}
	
	
	
	/**
	 * GET PRE DEFINED LINKS
	 */
	public static function getPredefinedLinks() {
		$predefinedLinks = array();
		$query = DB::getInstance()->query("
			SELECT
				id,
				preName,
				preLinkNoModRewrite,
				preLinkModRewrite
			FROM
				".Tables::$predefLink."
			ORDER BY
				preName ASC");
		
		while($p = DB::getInstance()->fetchArray($query)) {
			$predefinedLinks[] = $p;
		}
		return $predefinedLinks;
	}
	
	/**
	 * ADD PRE DEFINED LINK
	 */
	public static function addPredefinedLink($connId,$name,$linkNoMod,$linkMod) {
		DB::getInstance()->query("
			INSERT INTO
				".Tables::$predefLink."
			SET
				preConnectionId = '".DB::parseVar($connId)."',
				preName = '".DB::parseVar($name)."',
				preLinkNoModRewrite = '".DB::parseVar($linkNoMod)."',
				preLinkModRewrite = '".DB::parseVar($linkMod)."'");
	}
	
	/**
	 * EDIT PRE DEFINED LINK
	 */
	public static function editPredefinedLink($connId,$name,$linkNoMod,$linkMod) {
		DB::getInstance()->query("
			UPDATE
				".Tables::$predefLink."
			SET
				preName = '".DB::parseVar($name)."',
				preLinkNoModRewrite = '".DB::parseVar($linkNoMod)."',
				preLinkModRewrite = '".DB::parseVar($linkMod)."'
			WHERE
				preConnectionId = '".DB::parseVar($connId)."'");
	}
	
	
	/**
	 * DELETE PRE DEFINED LINK
	 */
	public static function deletePredefinedLink($connId) {
		DB::getInstance()->query("
			DELETE FROM
				".Tables::$predefLink."
			WHERE
				preConnectionId = '".DB::parseVar($connId)."'");
	}
	
	
	
	public static function parseLinkText($input) {
		//$input = urlencode($input);
		$letters = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','_','-','ä','ü','ö','ß','%',' ','(',')','&','"',"'","<",">","1","2","3","4","5","6","7","8","9","0");
		$output2 = "";
		for($i=0;$i<strlen($input);$i++) {
			if(in_array(mb_strtolower($input{$i}),$letters)) {
				$output2 .= $input{$i};
			}
		}

		if(strlen($output2) > 20) $output2 = substr($output2,0,20);
		return urlencode($output2);
	}
}
?>